package com.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project.bean.DeptBean;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDeptMapper extends BaseMapper<DeptBean> {
    @Select("SELECT d.*, COUNT(e.pk_emId) emNum FROM t_dept d LEFT JOIN t_employee e ON d.pk_deptId = e.fk_deptId\n" +
            "GROUP BY d.pk_deptId")
    @ResultMap("deptMap")
    public List<DeptBean> findAll();


    @Delete("DELETE FROM t_employee WHERE fk_deptId = #{id};" +
            "DELETE FROM t_dept WHERE pk_deptId = #{id};")
    public void delCasede(Integer id);

    @Delete("UPDATE t_employee SET fk_deptId = null WHERE fk_deptId =#{id};" +
            "DELETE from t_dept WHERE pk_deptId =#{id}")
    public void delSetNull(Integer id);
}
